# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, İlker Kesen <ilker.kde@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.

import variables as scroppy
import piksemel
import urllib2

class User:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/profile.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def username(self):
        return self.data.getAttribute("username")

    def realname(self):
        return self.data.getTagData("realname")

    def URL(self):
        return self.data.getTagData("url")

    def ID(self):
        return self.data.getAttribute("id")

    def cluster(self):
        return self.data.getAttribute("cluster")

    def date(self):
        return self.data.getTagData("registered")

    def age(self):
        return self.data.getTagData("age")

    def gender(self):
        return self.data.getTagData("gender")

    def country(self):
        return self.data.getTagData("country")

    def count(self):
        return self.data.getTagData("playcount")

    def avatar(self):
        return self.data.getTagData("avatar")

class TopArtists:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/topartists.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def topArtists(self):
        (topartists, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getTagData("name")
                count  = variable.getTagData("playcount")
                rank   = variable.getTagData("rank")
                url    = variable.getTagData("url")
                thumb  = variable.getTagData("thumbnail")
                image  = variable.getTagData("image")
                topartists.append((name, count, rank, url, thumb, image))
        return topartists

class TopAlbums:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/topalbums.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def topAlbums(self):
        (topalbums, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                artist = variable.getTagData("artist")
                name   = variable.getTagData("name")
                count  = variable.getTagData("playcount")
                rank   = variable.getTagData("rank")
                url    = variable.getTagData("url")
                small  = variable.getTag("image").getTagData("small")
                medium = variable.getTag("image").getTagData("medium")
                large  = variable.getTag("image").getTagData("large")
                topalbums.append((name, artist, count, rank, url, small, medium, large))
        return topalbums

class TopTracks:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/toptracks.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def topTracks(self):
        (toptracks, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                artist = variable.getTagData("artist")
                name   = variable.getTagData("name")
                count  = variable.getTagData("playcount")
                rank   = variable.getTagData("rank")
                url    = variable.getTagData("url")
                toptracks.append((name, artist, count, rank, url))
        return toptracks

class TopTags:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/tags.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def topTags(self):
        (toptags, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getTagData("name")
                count  = variable.getTagData("count")
                url    = variable.getTagData("url")
                toptags.append((name, count, url))
        return toptags

class Friends:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/friends.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def list(self):
        (friends, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getAttribute("username")
                url    = variable.getTagData("url")
                image  = variable.getTagData("image")
                friends.append((name, url, image))
        return friends

class Neighbours:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/neighbours.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def list(self):
        (neighbours, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getAttribute("username")
                url    = variable.getTagData("url")
                image  = variable.getTagData("image")
                neighbours.append((name, url, image))
        return neighbours

class RecentTracks:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/recenttracks.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def list(self):
        (tracks, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getTagData("name")
                artist = variable.getTagData("artist")
                url    = variable.getTagData("url")
                date   = variable.getTagData("date")
                tracks.append((name, artist, url, date))
        return tracks

class RecentBannedTracks:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/recentbannedtracks.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def list(self):
        (tracks, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getTagData("name")
                artist = variable.getTagData("artist")
                url    = variable.getTagData("url")
                date   = variable.getTagData("date")
                tracks.append((name, artist, url, date))
        return tracks

class RecentLovedTracks:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/recentlovedtracks.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def list(self):
        (tracks, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getTagData("name")
                artist = variable.getTagData("artist")
                url    = variable.getTagData("url")
                date   = variable.getTagData("date")
                tracks.append((name, artist, url, date))
        return tracks

class WeeklyChartList:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/weeklychartlist.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def list(self):
        (lists, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                _from  = variable.getAttribute("from")
                to     = variable.getAttribute("to")
                lists.append((_from, to))
        return lists

class WeeklyArtistChart:
    def __init__(self, user, _from = False, to = False):
        user = user.replace(" ", "%20")
        file = "weeklyartistchart.xml"
        if _from and to:
            file += "?from=%s&to=%s" % (_from, to)
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/%s" % (scroppy.url, scroppy.scrobblerVersion, user, file)).read())

    def user(self):
        return self.data.getAttribute("user")

    def _from(self):
        return self.data.getAttribute("from")

    def to(self):
        return self.data.getAttribute("to")

    def list(self):
        (artists, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name  = variable.getTagData("name")
                rank  = variable.getTagData("chartposition")
                count = variable.getTagData("playcount")
                url   = variable.getTagData("url")
                artists.append((name, rank, count, url))
        return artists

class WeeklyAlbumChart:
    def __init__(self, user, _from = False, to = False):
        user = user.replace(" ", "%20")
        file = "weeklyalbumchart.xml"
        if _from and to:
            file += "?from=%s&to=%s" % (_from, to)
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/%s" % (scroppy.url, scroppy.scrobblerVersion, user, file)).read())

    def user(self):
        return self.data.getAttribute("user")

    def _from(self):
        return self.data.getAttribute("from")

    def to(self):
        return self.data.getAttribute("to")

    def list(self):
        (albums, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                artist = variable.getTagData("artist")
                name   = variable.getTagData("name")
                rank   = variable.getTagData("chartposition")
                count  = variable.getTagData("playcount")
                url    = variable.getTagData("url")
                albums.append((artist, name, rank, count, url))
        return albums

class WeeklyTrackChart:
    def __init__(self, user, _from = False, to = False):
        user = user.replace(" ", "%20")
        file = "weeklytrackchart.xml"
        if _from and to:
            file += "?from=%s&to=%s" % (_from, to)
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/%s" % (scroppy.url, scroppy.scrobblerVersion, user, file)).read())

    def user(self):
        return self.data.getAttribute("user")

    def _from(self):
        return self.data.getAttribute("from")

    def to(self):
        return self.data.getAttribute("to")

    def list(self):
        (tracks, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                artist = variable.getTagData("artist")
                name   = variable.getTagData("name")
                rank   = variable.getTagData("chartposition")
                count  = variable.getTagData("playcount")
                url    = variable.getTagData("url")
                tracks.append((artist, name, rank, count, url))
        return tracks

class Recommendations:
    def __init__(self, user):
        user = user.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/systemrecs.xml" % (scroppy.url, scroppy.scrobblerVersion, user)).read())

    def user(self):
        return self.data.getAttribute("user")

    def list(self):
        (artists, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getTagData("name")
                url    = variable.getTagData("url")
                artists.append((name, url))
        return artists

class TasteOMeter:
    def __init__(self, who, with):
        who, with = who.replace(" ", "%20"), with.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/user/%s/tasteometer.xml?with=%s" % (scroppy.url, scroppy.scrobblerVersion, who, with)).read())

    def who(self):
        return self.data.getAttribute("a")

    def with(self):
        return self.data.getAttribute("b")

    def score(self):
        return self.data.getTagData("score")

    def commonArtists(self):
        (artists, tags, variable) = ([], self.data.getTag("commonArtists").tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                artist = variable.getTagData("name")
                artists.append((artist))
        return artists